home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CPPWIN10.ARJ / EXHPP.C < prev    next >
C/C++ Source or Header  |  1990-10-25  |  3KB  |  148 lines

  1. /***
  2.     HPP Extract utility.
  3.     This program extracts the .HPP file out of a .CPP file and compares
  4.     it with the .HPP file already on disk. If different, the new one
  5.     will replace the old one.
  6. ***/
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. #define FALSE    0
  12. #define TRUE    -1
  13.  
  14. void CheckFile(FILE *pOFile, char *pName);
  15. void ExHpp(char *pName);
  16. int SearchLine(register char *pbuf, register char *psrch);
  17.  
  18.     /** Global variables */
  19. int erctr;
  20. char buf[512];
  21.  
  22. int main(int argc, char **argv)
  23. {
  24.     register int i;
  25.  
  26.     if (argc < 2) {
  27.         fprintf(stderr, "usage: exhpp <filename> <filename>....\n");
  28.         exit(1);
  29.     }
  30.     puts("EXHPP : HPP Extract utility version 1.00 (c) 1990 Comptech Software,Inc.");
  31.     erctr = 0;
  32.     for (i = 1; i < argc; i++)
  33.         ExHpp(argv[i]);
  34.  
  35.     return erctr;
  36. }
  37.  
  38. /***
  39.     Extract the HPP section for the passed file name.
  40. ***/
  41. void ExHpp(char *pName)
  42. {
  43.     FILE *pFile;
  44.     FILE *pOFile;
  45.     int state = 0;
  46.  
  47.     pFile = fopen(pName, "r");
  48.     if (pFile == NULL) {
  49.         erctr++;
  50.         fprintf(stderr, "Can't open file \"%s\"\n", pName);
  51.         return;
  52.     }
  53.     pOFile = fopen("HPPEX.$$$", "w");
  54.     if (pOFile == NULL) {
  55.         erctr++;
  56.         fprintf(stderr, "Can't create temp file\n");
  57.         return;
  58.     }
  59.     while (fgets(buf, sizeof(buf), pFile) != NULL) {
  60.         if (state == 0) {
  61.             if (SearchLine(buf, "$HPP:Start") == TRUE) {
  62.                 state = 1;
  63.             }
  64.             continue;
  65.         }
  66.         else if (state == 1) {
  67.             if (SearchLine(buf, "$HPP:End") == TRUE) {
  68.                 state = 2;
  69.                 break;
  70.             }
  71.         }
  72.         fputs(buf, pOFile);
  73.     }
  74.     fclose(pFile);
  75.     if (state == 0) {
  76.         fprintf(stderr, "No HPP section found in file \"%s\"\n", pName);
  77.     }
  78.     else if (state == 1) {
  79.         fprintf(stderr, "No $HPP:End section found in file \"%s\"\n", pName);
  80.     }
  81.     else
  82.         CheckFile(pOFile, pName);
  83. }
  84.  
  85. /***
  86.     Check the file
  87. ***/
  88. void CheckFile(FILE *pOFile, char *pName)
  89. {
  90.     char buf2[512];
  91.     char fname[66];
  92.     char *ptr;
  93.     FILE *pHFile;
  94.     int flag = FALSE;
  95.  
  96.     fflush(pOFile);         /* Flush file */
  97.     fseek(pOFile, 0l, 0);    /* Seek to beginning of file */
  98.     strcpy(fname, pName);
  99.     ptr = strrchr(fname, '.');
  100.     if (ptr)
  101.         strcpy(ptr, ".HPP");
  102.     else
  103.         strcat(fname, ".HPP");
  104.     pHFile = fopen(fname, "r");
  105.     if (pHFile == NULL) {        /* .HPP file doesn't exist */
  106.         fclose(pOFile);
  107.         goto DoRename;
  108.     }
  109.     while (fgets(buf, sizeof(buf), pHFile) != NULL) {
  110.         if (fgets(buf2, sizeof(buf2), pOFile) == NULL) {
  111.             flag = TRUE;
  112.             break;
  113.         }
  114.         if (strcmp(buf, buf2)) {
  115.             flag = TRUE;
  116.             break;
  117.         }
  118.     }
  119.     fclose(pOFile);
  120.     fclose(pHFile);
  121.     if (flag == TRUE) {         /* Files are different */
  122.         unlink(fname);            /* Kill old HPP file */
  123. DoRename:
  124.         rename("HPPEX.$$$", fname); /* rename temp file to .HPP file name */
  125.         return;
  126.     }
  127.     else
  128.         unlink("HPPEX.$$$");    /* Kill the temp name */
  129. }
  130.  
  131. /***
  132.     Search for the psrch string in the pbuf buffer.
  133.     return TRUE if found.
  134. ***/
  135. int SearchLine(register char *pbuf, register char *psrch)
  136. {
  137.     int i = strlen(psrch);
  138.  
  139.     while (*pbuf) {
  140.         if (strncmp(pbuf, psrch, i) == 0)
  141.             return TRUE;
  142.         else
  143.             pbuf++;
  144.     }
  145.     return FALSE;
  146. }
  147.  
  148.